package com.a.a;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;

class f extends CharsetDecoder {
    private final a a;
    private final byte b;
    private final byte c;
    private final boolean d;
    private boolean e;
    private int f;
    private int g;
    private boolean h;
    private boolean i;

    f(e eVar, a aVar, boolean z) {
        super(eVar, 0.6f, 1.0f);
        this.a = aVar;
        this.d = z;
        this.b = eVar.a();
        this.c = eVar.b();
    }

    private CoderResult a(ByteBuffer byteBuffer) {
        byteBuffer.position(byteBuffer.position() - 1);
        return CoderResult.OVERFLOW;
    }

    private CoderResult a(ByteBuffer byteBuffer, CharBuffer charBuffer, byte b) {
        CoderResult coderResult = null;
        int a = this.a.a(b);
        if (a >= 0) {
            this.f += 6;
            if (this.f < 16) {
                this.g = (a << (16 - this.f)) + this.g;
                return null;
            }
            this.f -= 16;
            this.g += a >> this.f;
            charBuffer.put((char) this.g);
            this.g = (a << (16 - this.f)) & 65535;
            return null;
        } else if (this.d) {
            return b(byteBuffer);
        } else {
            charBuffer.put((char) b);
            if (a()) {
                coderResult = b(byteBuffer);
            }
            b();
            return coderResult;
        }
    }

    private boolean a() {
        return this.g != 0 || this.f >= 6;
    }

    private CoderResult b(ByteBuffer byteBuffer) {
        byteBuffer.position(byteBuffer.position() - 1);
        return CoderResult.malformedForLength(1);
    }

    private void b() {
        this.e = false;
        this.f = 0;
        this.g = 0;
    }

    protected CoderResult decodeLoop(ByteBuffer byteBuffer, CharBuffer charBuffer) {
        while (byteBuffer.hasRemaining()) {
            byte b = byteBuffer.get();
            if (this.e) {
                if (b == this.c) {
                    if (a()) {
                        return b(byteBuffer);
                    }
                    if (!this.h) {
                        this.i = true;
                    } else if (!charBuffer.hasRemaining()) {
                        return a(byteBuffer);
                    } else {
                        charBuffer.put((char) this.b);
                    }
                    b();
                } else if (!charBuffer.hasRemaining()) {
                    return a(byteBuffer);
                } else {
                    CoderResult a = a(byteBuffer, charBuffer, b);
                    if (a != null) {
                        return a;
                    }
                }
                this.h = false;
            } else if (b == this.b) {
                this.e = true;
                if (this.i && this.d) {
                    return b(byteBuffer);
                }
                this.h = true;
            } else if (!charBuffer.hasRemaining()) {
                return a(byteBuffer);
            } else {
                charBuffer.put((char) b);
                this.i = false;
            }
        }
        return CoderResult.UNDERFLOW;
    }

    protected CoderResult implFlush(CharBuffer charBuffer) {
        return ((this.e && this.d) || a()) ? CoderResult.malformedForLength(1) : CoderResult.UNDERFLOW;
    }

    protected void implReset() {
        b();
        this.i = false;
    }
}
